package com.xdxc.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xdxc.mapper.ProcessedBatchMapper;
import com.xdxc.model.entity.ProcessedBatch;
import com.xdxc.service.serviceinterface.ProcessedBatchService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service // 标记为 Spring 的服务组件
public class ProcessedBatchServiceImpl
        extends ServiceImpl<ProcessedBatchMapper, ProcessedBatch>
        implements ProcessedBatchService {

    @Autowired
    private ProcessedBatchMapper processedBatchMapper;

    /**
     * 检查批次是否已处理
     */
    @Override
    public boolean isProcessed(String batchId) {
        return processedBatchMapper.existsByBatchId(batchId);
    }

    /**
     * 标记批次为已处理（事务方法）
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public void markProcessed(String batchId, Long examId) {
        if (isProcessed(batchId)) {
            return;
        }
        ProcessedBatch entity = new ProcessedBatch();
        entity.setBatchId(batchId);
        entity.setExamId(examId);
        processedBatchMapper.insert(entity);
    }
}